<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
		public function Index(){
			$this -> display();
		}
		
	public function login() {
		if(IS_POST){
            $username=$_POST["loginname"];
            $password=md5($_POST["password"]);
		    $m=M("Member");
			$user=$m->field("id,username,name")->where("username='{$username}' and password='{$password}'")->find();
			if($user){
				//查询用户限权
				$user['rules']=$this->getRulesByUserID($user['id']);
				$user['code']=11;
				session_start();
				$_SESSION['username']=$username;
				$this->ajaxReturn($user);
			}else{
				$obj=(object)[];
			    $obj->code=0;
			    $obj->message="登陆失败";
				$this->ajaxReturn($obj);
			}
		}else{
			$this -> display();
		}
	}
      
	private function getRulesByUserID($uid){
		$access=M('auth_group_access');
		$group=$access->field('rules')->join('think_auth_group on think_auth_group.id=think_auth_group_access.group_id')->where('uid='.$uid)->find();
	    $rule=M('auth_rule');
		$userRelus=$rule->field("id,name,title,pid")->where('id in('.$group['rules'].') and id_menu=1')->select();
	    return $userRelus;
	}  
	  
	public function main() {
		$this -> display();
	}

}
